<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="artDialog/skins/default.css" type="text/css"></link>
<link href="css/select.css" rel="stylesheet" type="text/css" />
<title>DEMO</title>
<script type="text/javascript" src="js/apiv1.3.min.js"></script>
<!--script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script-->
<link rel="stylesheet" type="text/css" href="bmap.css" />
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/select-ui.min.js"></script>
<script type="text/javascript" src="artDialog/artDialog.min.js"></script>
<script type="text/javascript" src="artDialog/jquery.artDialog.min.js"></script>
<script type="text/javascript" src="artDialog/artDialog.plugins.min.js"></script>
<script type="text/javascript" src="js/util.js"></script>

</head>
<body>
	<div class="place">
		<span>位置：</span>
		<ul class="placeul">
			<li><a href="#">首页</a>
			</li>
			<li><a href="#">巡检任务管理</a>
			</li>
			<li><a href="#">巡检任务制定</a>
			</li>
		</ul>
	</div>

	<div
		style="width:100%;height:70%;border:1px solid gray;position:absolute;"
		id="container"></div>
		
		<div id="addRoute" style="position:absolute;top:80%;left:0%;">
		任务编号:<input type="text" id="rwbh" class="dfinput" style="width:120px;" />
		任务名称:<input type="text" id="rwmc" class="dfinput" style="width:80px;" />
		路线名称:<input type="text" id="lxmc" class="dfinput" style="width:120px;" disabled="disabled"/>
		路线ID:<input type="text" id="lxid" class="dfinput" style="width:80px;" disabled="disabled"/>
		<input type="button"  class="btn"  value="设置起始杆塔" id="btn_start" onclick="setStart()" />
		<input type="text" id="sno" class="dfinput" disabled="disabled" style="width:70px;" value="起始杆塔"/>
		<label id="startNO" style="display: none"></label>
		<input type="button"  class="btn"   value="设置终止杆塔" id="btn_end" onclick="setEnd()" />
		<input type="text" id="eno" class="dfinput" disabled="disabled" style="width:70px;" value="终止杆塔" />
		 <label id="endNO"  style="display: none"></label><p><br></p>
		 <label id="user_ids" style="display: none"></label><label id="route_length" style="display: none"></label>
		巡检员：<input type="text" id="userlist" class="dfinput"  disabled="disabled"/>
		 <input type="button" class="btn" value="添加巡检员" id="set_users" onclick="setUsers()" />
		 <input type="button" class="btn" value="添加任务" id="add_task" onclick="add_task()" />
		<label id="uids" style="display: none"></label>			

	</div>
</body>
</html>
<script type="text/javascript">
	var mapOptions = {
		mapType : BMAP_NORMAL_MAP,
		maxZoom : 10, //地图最大层级
		minZoom : 5
	}
	var map = new BMap.Map("container", mapOptions); //设置卫星图为底图BMAP_PERSPECTIVE_MAP
	var initPoint = new BMap.Point(116.404, 39.915); // 创建点坐标

	map.centerAndZoom(initPoint, 8); // 初始化地图,设置中心点坐标和地图级别。
	map.enableScrollWheelZoom(); // 启用滚轮放大缩小。
	map.enableKeyboard(); // 启用键盘操作。  
	map.enableContinuousZoom(); //启用连续缩放效果

	// ----- control -----
	map.addControl(new BMap.NavigationControl()); //地图平移缩放控件
	map.addControl(new BMap.ScaleControl()); //显示比例尺在右下角
	

	// ----- Hide or Show Pole -----
	map.addEventListener("zoomend", function() {
		var overlays = map.getOverlays();
		for ( var i = 0; i < overlays.length - 1; i++) {
			if (overlays[i]._config.icon) {
				if (overlays[i]._config.icon.imageUrl.indexOf("images/circle")!=-1) {
					if (map.getViewport().zoom <= 7) {
						overlays[i].hide();
					} else {
						overlays[i].show();
					}
				}
			}
		}
	});
	



	function addPole(lng, lat, opts,color) {
		var point = new BMap.Point(lng, lat);
		var myIcon = new BMap.Icon("images/circle"+color+".png", new BMap.Size(16, 16));

		var marker = new BMap.Marker(point, {
			icon : myIcon,
			enableDragging : false,
			draggingCursor : "move",
			title : opts
		});
		
		/*	var contextMenu = new BMap.ContextMenu();
			contextMenu.addItem("删除杆塔", function() {
				alert("del");
			});
			marker.addContextMenu(contextMenu);
		 */
		 
		marker.addEventListener("mouseover", function(type, target, point,
				pixel) {
			var x = type.pixel.x;
			var y = type.pixel.y;
			$("body").append("<div id =\"routeDiv\"></div>")
			routeDiv = $("#routeDiv").css("position", "absolute").css("top", y)
					.css("left", x).css("z-index", 1);
			routeDiv.html("<span style=\"font-size:12px;font-weight:bolder\">"
					+ opts + "</span>");
		});
		marker.addEventListener("mouseout", function() {
			if (routeDiv) {
				routeDiv.remove();
			}
		});
		
		
		map.addOverlay(marker);
		return marker;
	}
	
	
	map.addEventListener("click",selectRoute);
	function selectRoute(type){
	if(!type.overlay){
		return;
	}
	
	
	$.post("route!findByPid.action",{pid:type.overlay._config.title},function(data){
				var arr = eval("("+data+")");
				if(arr.length>1){
					var routeSelect = "<select class=\"routeSelect\" id='routeSelect'>";
					for(var i = 0;i<arr.length;i++){
						routeSelect+="<option value='"+arr[i].id+"'>"+arr[i].name+"</option>"
					}
					routeSelect+="</select>";
					
					art.dialog({title:"选择路线",content:routeSelect,lock:true,fixed:true,okValue:"选择",cancelValue:"取消",ok:function(){
						$("#routeSelect").children().each(function(){
							if($(this).attr("selected")){
								$("#lxmc").val($(this).text());
								$("#lxid").val($(this).val());
								map.removeEventListener("click",selectRoute);
								hideRoute($(this).val());
							}
						});
						return true;
					},cancel:function(){}});
					$(".routeSelect").uedSelect({width:280});
					
				}else{
					$("#lxmc").val(arr[0].name);
					$("#lxid").val(arr[0].id);
					map.removeEventListener("click",selectRoute);
					hideRoute(arr[0].id);
				}
			});
	}
	
	
	function hideRoute(id){
	$.post("route!findPoliesByCid.action?cid="+id,function(data){
		var points=eval("("+data+")");
		var overlays = map.getOverlays();
			for ( var i = 0; i < overlays.length ; i++) {
			if (overlays[i]._config&&overlays[i]._className=="Marker") {
				var flag=true;
				for(var x=0;x<points.length;x++){
					if(points[x].lng==overlays[i]._config.point.lng&&points[x].lat==overlays[i]._config.point.lat){
						flag=false;
						break;
					}
				}
				if(flag){
					map.removeOverlay(overlays[i]);
				}
			}
		}
	
	});
		
	}
	
	
	var routeDiv;
	function addRoute(points, opts, color) {

		var polyline = new BMap.Polyline(points, {
			strokeColor : color,
			strokeWeight : 3,
			strokeOpacity : 0.8,
			enableClicking :true
		});
		
		polyline.addEventListener("mouseover", function(type, target, point,
				pixel) {
			var x = type.pixel.x;
			var y = type.pixel.y;
			$("body").append("<div id =\"routeDiv\"></div>")
			routeDiv = $("#routeDiv").css("position", "absolute").css("top", y)
					.css("left", x).css("z-index", 1);
			routeDiv.html("<span style=\"font-size:12px;font-weight:bolder\">"
					+ opts + "</span>");
					
		});
		
		
		polyline.addEventListener("mouseout", function() {
			if (routeDiv) {
				routeDiv.remove();
			}
			
		});
		
		
		map.addOverlay(polyline);
	}


	function setStart() {
	
		map.removeEventListener("click", start_click);
		map.addEventListener("click", start_click);
	}
	
	
	function setEnd() {
	
		map.removeEventListener("click", end_click);
		map.addEventListener("click", end_click);
	}

	
	
	
	function calc_length(html) {
		var arr = html.split("<br>");
		var spoint = getPointByPointTitle($("#startNO").html());//获取起点point
		var epoint = getPointByPointTitle($("#endNO").html());//获取终点point
		var length = 0;
		if(arr.length>1){
			length=map.getDistance(spoint,getPointByPointTitle(arr[0]));
			
			for(var m = 1;m<arr.length;m++){
				length+=map.getDistance(getPointByPointTitle(arr[m-1]),getPointByPointTitle(arr[m]));
			}
			
			length +=map.getDistance(getPointByPointTitle(arr[arr.length-1]),epoint);
			
		}else{
			length=map.getDistance(spoint,epoint);
		}
		length =(length/1000)+"";
		var tmp = length.split(".");
		length = tmp[0]+"."+tmp[1].substring(0,3)+"公里";
		
		return length;	
	}
	
	
	function getPointByPointTitle(title){
		var overlays = map.getOverlays();
			for ( var i = 0; i < overlays.length ; i++) {
			if (overlays[i]._config) {
				if (overlays[i]._config.title==title) {
					return overlays[i]._config.point;
				}
			}
		}
	}
	
	function end_click(type) {
		okCancelAlert("设置终止杆塔","确定将:" + type.overlay._config.title + "设置为终止杆塔吗？",function(){
				$("#endNO").html(type.overlay._config.title);
				$("#eno").val(type.overlay._config.title);
				map.removeEventListener("click", end_click);
				$("#btn_end").attr("disabled", true);
				$("#set_users").attr("disabled",false);
				$("#add_task").attr("disabled",false);
		
		
		},function(){});
	}
	
	
	function start_click(type) {
	okCancelAlert("设置起始杆塔","确定将:" + type.overlay._config.title + "设置为起始杆塔吗？",function(){
		$("#startNO").html(type.overlay._config.title);
			$("#sno").val(type.overlay._config.title);
			map.removeEventListener("click", start_click);
			$("#btn_start").attr("disabled", "disabled");
			$("#btn_end").attr("disabled", false);
	},function(){});
		
	}
	
	$.post("pole!findList.action", function(data) {
		var polies = eval("("+data+")");
		var arr=["","_red","_yellow"];
		for ( var i = 0; i < polies.length; i++) {
			addPole(polies[i].lng, polies[i].lat, polies[i].id,"");
		}
	});
	
	
//findAllPolies
	$.post("route!findAllPolies.action",function(data){
			var json = eval("("+data+")");
			for(var i=0;i<json.length;i++){
				var polies = json[i];
				var points = new Array();
				var name = "";
				var points1 = new Array();
				for ( var x = 0; x < polies.length; x++) {
					points[x] = new BMap.Point(polies[x].lng,polies[x].lat);
					name = polies[x].routeName;
				}
				addRoute(points, name, "green");
  				
			}
	});


	
	function setUsers(){
		$.post("user!findByGName.action",{name:"巡检员"},function(data){
			var routeSelect = "<select class=\"routeSelect\" id='routeSelect'>";
					var arr= eval("("+data+")");
					var ids = $("#uids").html();
					if(ids!=""){
						ids+=","
					}
					var count =0;
					for(var i = 0;i<arr.length;i++){
						if(ids.indexOf(arr[i].id+",")==-1){
							routeSelect+="<option value='"+arr[i].id+"'>"+arr[i].name+"</option>";
							count++;
						}
					}
					if(count==0){
						defaultAlert("添加巡检员","已经没有人可以选了");
						return;
					}
					routeSelect+="</select>";
					
					art.dialog({title:"选择巡检员",content:routeSelect,lock:true,fixed:true,okValue:"选择",cancelValue:"取消",ok:function(){
						$("#routeSelect").children().each(function(){
							if($(this).attr("selected")){
								var names = $("#userlist").val();
								var ids = $("#uids").html();
								if(names!=""){
									names+=",";
									ids+=",";
								}
								names+=$(this).text();
								ids+=$(this).val();
								$("#userlist").val(names);
								$("#uids").html(ids);
								map.removeEventListener("click",selectRoute);
								/* hideRoute($(this).val()); */
							}
						});
						return true;
					},cancel:function(){}});
					$(".routeSelect").uedSelect({width:280});
		});
	}
	
function add_task(){
    console.info($("#lxid").val());
    debugger;
    if($("#rwbh").val()==""){
		alert("任务编号不能为空");
		return false;
    }
    if($("#rwmc").val()==""){
    	alert("任务名称不能为空");
		return false;
    }
    if($("#lxmc").val()==""){
    	alert("路线名称不能为空,请在线路中设置塔杆");
		return false;
    }
    if($("#lxid").val()==""){
    	alert("路线ID不能为空,请在线路中设置塔杆");
		return false;
    }
	$.post("patrolTask!addTask.action",{
		"patroltask.id" :$("#rwbh").val(),
		"patroltask.name":$("#rwmc").val(),
		"patroltask.cid":$("#lxid").val(),
		"patroltask.startPoleNo":$("#sno").val(),
		"patroltask.endPoleNo":$("#eno").val(),
		"uid":$("#uids").html()
	},function(data){
		var json = eval("("+data+")");
				if(json.code==1){
					defaultAlert("制定巡检任务","制定成功");
					setTimeout(function () { 
						location.reload(true);
					}, 2000);
					
				}else if(json.code==3){
					defaultAlert("制定巡检任务","此路线已制定");
				}else{
					defaultAlert("制定巡检任务","制定失败");	
				}
	});
}


	$(function() {
		$("#btn_start").attr("disabled", false);
		$("#btn_end").attr("disabled", true);
		$("#set_users").attr("disabled",true);
		$("#add_task").attr("disabled",true);
		
	});
	
	
</script>
